﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CKEditor.NET;
using WebUI.BL.News;
using WebUI.DAL;

namespace WebUI.AdminSite.News
{
    public partial class NewsCategoryAddLanguage : System.Web.UI.Page
    {
        public int IdEntity { get; set; }
        private static bool addNew = true; //true la duoc them moi, false la update
        protected void Page_Load(object sender, EventArgs e)
        {
            hdfId.Value = Request.QueryString["id"];
            IdEntity = String.IsNullOrEmpty(hdfId.Value) ? 0 : int.Parse(hdfId.Value);
            if(!IsPostBack)
            {
                if (IdEntity > 0)
                {
                    addNew = true;
                    BindingModel();
                }
            }
        }

        private void BindingModel()
        {
            var repository=new NewsCategoryBL();
            var model = repository.GetStoreLangugageAttributes(2, IdEntity);
            if(model.Any())
            {
                addNew = false;

                TextBox txtName1 = (TextBox)UcNameDescription1.FindControl("txtName");
                txtName1.Text = model.Where(x => x.Attrribute == "Name").Select(n => n.ValueAttr).FirstOrDefault();
                
                hdfIdName.Value = model.Where(x => x.Attrribute == "Name").Select(n => n.Id).FirstOrDefault().ToString();

                CKEditorControl ck1 = (CKEditorControl)UcNameDescription1.FindControl("ckey1");
                ck1.Text = model.Where(x => x.Attrribute == "Description").Select(n => n.ValueAttr).FirstOrDefault();
                
                hdfIdDescription.Value = model.Where(x => x.Attrribute == "Description").Select(n => n.Id).FirstOrDefault().ToString();
            } 
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var repository = new NewsCategoryBL();
            try
            {
                if (addNew)//them moi
                {
                    var modelName = new tbl_StoreLanguageAtrribute();
                    modelName.Attrribute = "Name";
                    modelName.ValueAttr = ((TextBox)UcNameDescription1.FindControl("txtName")).Text; 
                    modelName.GroupAttribute = "NewsCategory";
                    modelName.EntityId = IdEntity;
                    modelName.IsDeleted = false;
                    modelName.LangId = 2;


                    var modelDescription = new tbl_StoreLanguageAtrribute();
                    modelDescription.Attrribute = "Description";
                    modelDescription.ValueAttr = ((CKEditorControl)UcNameDescription1.FindControl("ckey1")).Text;
                    modelDescription.GroupAttribute = "NewsCategory";
                    modelDescription.EntityId = IdEntity;
                    modelDescription.IsDeleted = false;
                    modelDescription.LangId = 2;

                    repository.SaveStoreEnglish(modelName,addNew);
                    repository.SaveStoreEnglish(modelDescription, addNew);
                    addNew = false;
                }
                else //update
                {
                    int IdName = int.Parse(hdfIdName.Value);
                    var modelNameUpdate = repository.GetStoreEnglishById(IdName);
                    if(modelNameUpdate!=null)
                    {
                        modelNameUpdate.Attrribute = "Name";
                        modelNameUpdate.ValueAttr = ((TextBox)UcNameDescription1.FindControl("txtName")).Text;
                    }

                    int IdDescription = int.Parse(hdfIdDescription.Value);
                    var modelDescriptionUpdate = repository.GetStoreEnglishById(IdDescription);
                    if (modelDescriptionUpdate != null)
                    {
                        modelDescriptionUpdate.Attrribute = "Description";
                        modelDescriptionUpdate.ValueAttr = ((CKEditorControl)UcNameDescription1.FindControl("ckey1")).Text;
                    }
                    repository.SaveStoreEnglish(modelNameUpdate, addNew);
                    repository.SaveStoreEnglish(modelDescriptionUpdate, addNew);
                    addNew = false;
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }
    }
}